<?php


namespace App\Http\Controllers\Admin\Critical;


use App\Enums\CriticalEnum;
use App\Enums\RedisKeyEnum;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use App\Services\Tools\HelperService;
use App\Traits\AdminLog;
use Illuminate\Http\Request;
use Symfony\Component\Console\Helper\Helper;

//暴击奖池配置
class CriticalPoolConfigController extends BaseController
{
    use AdminLog;
    private $moduleId = CriticalEnum::MODULE_TREE;

    /**
     * @api {put} /critical/ratio 设置暴击奖池比例
     * @apiGroup  暴击奖池
     * @apiParam {floor} ratio 比率(百分之，传整数 如 0.05传 5)
     * @apiParam {Int} pool_type 奖池类型(十连百连--1 特殊个人--40 个人独立--50 )
     * @apiParam {Int} type_id 类型id
     * @apiSuccessExample {json} Response-Example
     * {
     * "code": 200,
     * "data": [],
     * "message": "设置成功"
     * }
     */
    public function set(Request $request)
    {
        $param = $request->only('ratio', 'pool_type', 'type_id');
        $v = app('validator')->make($param, [
            'ratio'     => 'required|integer|min:0',
            'pool_type' => 'required|in:1,40,50',
            'type_id'   => 'required|integer',
        ]);
        if ($v->fails()) {
            throw new BasicException(0, $v->errors()->first());
        }
        $ratio = $param['ratio'];
        if ($ratio > 10000) {
            throw new BasicException(0, '比率最大为1');
        }
        $poolType = $param['pool_type'] ?? 0;
        $typeId = $param['type_id'] ?? 0;
//        if ($poolType == CriticalEnum::TREE_COMMON_POOL) {
//            $typeId = 0;
//        }
        $data = [
            'module'    => $this->moduleId,
            'pool_type' => $poolType,
            'type_id'   => $typeId,
        ];
        $config = app('model_critical_pool_config')->where($data)->first();
        $config = empty($config) ? [] : $config->toArray();
        if (empty($config)) {
            $data['ratio'] = $ratio;
            $res = app('model_critical_pool_config')->insert($data);
        } else {
            if ($config['ratio'] == $ratio) {
                return Response::sendData([], '设置成功');
            }
            $res = app('model_critical_pool_config')->where('id', $config['id'])->update([
                'ratio' => $ratio,
            ]);
        }
        if (empty($res)) {
            throw new BasicException(0, '设置失败');
        }
        return Response::sendData([], '设置成功');
    }


    /**
     * @api {get} /critical/ratio 获取暴击奖池比率
     * @apiGroup  暴击奖池
     * @apiParam {Int} pool_type 奖池类型(十连百连--1 特殊个人--40 个人独立--50 )
     * @apiParam {Int} type_id 类型id
     * @apiSuccessExample {json} Response-Example
     * {
     * "code": 200,
     * "data": {
     * "ratio 入池比率单位百分之": 5
     * },
     * "message": ""
     * }
     */
    public function getRatio(Request $request)
    {
        $param = $request->only('pool_type', 'type_id');
        $v = app('validator')->make($param, [
            'pool_type' => 'required|in:1,40,50',
            'type_id'   => 'required|integer',
        ]);
        if ($v->fails()) {
            throw new BasicException(0, $v->errors()->first());
        }
        $poolType = $param['pool_type'] ?? 0;
        $typeId = $param['type_id'] ?? 0;
//        if ($poolType == CriticalEnum::TREE_COMMON_POOL) {
//            $typeId = 0;
//        }
        $data = [
            'module'    => $this->moduleId,
            'pool_type' => $poolType,
            'type_id'   => $typeId,
        ];
        $config = app('model_critical_pool_config')->where($data)->first();
        $config = empty($config) ? [] : $config->toArray();
        $ratio = empty($config) ? CriticalEnum::TREE_DEFAULT_RATIO : $config['ratio'];
        return Response::sendData([
            'ratio' => $ratio,
        ]);
    }

    
}